﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using MYLSOFT.JsonSimple;

namespace com.byt.webcore.utils
{
    public class DbJson
    {

        string[] colName;
        Type[] colType;
        int numcols;
        object[] values;
        DbDataReader dr;

        public DbJson(DbDataReader dr)
        {
            DataTable meta = dr.GetSchemaTable();
            numcols = dr.FieldCount;
            colName = new string[numcols];
            colType = new Type[numcols];

            // Construir el Nombre de las Columnas
            string cname;
            int col = 0;
            //Para cada campo de la tabla...
            foreach (DataRow myField in meta.Rows)
            {
                //Para cada propiedad del campo...
                cname = myField[0].ToString();
                colName[col] = cname.ToLower().Replace("_", "");
                colType[col] = (Type)myField[5];
                col++;
            }
            values = new object[numcols];
            this.dr = dr;
        }

        public JSONObject ToObject()
        {
            JSONObject jo = new JSONObject();
            try
            {
                dr.Read();
                dr.GetValues(values);
            }
            catch
            {
                return jo;
            }

            for (int i = 0; i < numcols; i++ )
            {
                jo.Add(colName[i], getObjectFromTable(colType[i], values[i]));
            }
            return jo;
        }

        public JSONArray ToArrayObject()
        {
            JSONArray ja = new JSONArray();

            while (dr.Read())
            {
                dr.GetValues(values);
                JSONObject jo = new JSONObject();
                for (int i = 0; i < numcols; i++)
                {
                    jo.Add(colName[i], getObjectFromTable(colType[i], values[i]));
                }
                ja.Add(jo);
            }
            return ja;
        }

        public JSONArray ToArrayArray()
        {
            JSONArray ja = new JSONArray();

            while (dr.Read())
            {
                dr.GetValues(values);
                JSONArray jit = new JSONArray();
                for (int i = 0; i < numcols; i++)
                {
                    jit.Add(getObjectFromTable(colType[i], values[i]));
                }
                ja.Add(jit);
            }
            return ja;
        }

        public JSONArray ToTree()
        {
            JSONArray ja = new JSONArray();

            while (dr.Read())
            {
                dr.GetValues(values);
                JSONArray jit = new JSONArray();
                for (int i = 0; i < numcols; i++)
                {
                    jit.Add(getObjectFromTable(colType[i], values[i]));
                }
                ja.Add(jit);
            }
            return ja;
        }

        private object getObjectFromTable(Type type, object p)
        {
            if (System.DBNull.Value == p)
                return null;
            else if (type == typeof(DateTime))
                return Utils.parseDate((DateTime)p);
            return p;
        }

    }
}
